package com.adream.scalpel.app.client.ui;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class ScalpelMenuTree extends Composite {
    private Scalpel scalpel;
    private TreeItem addTreeItem;
    private TreeItem editTreeItem;
    private TreeItem searchTreeItem;
    private TreeItem usersTreeItem;
    private TreeItem homeTreeItem;
    private Tree tree;

    /**
     * @wbp.parser.constructor true
     */
    public ScalpelMenuTree() {
    }

    public ScalpelMenuTree(Scalpel scalpel) {
        tree = new Tree();
        initWidget(tree);
        tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
            public void onSelection(SelectionEvent<TreeItem> event) {
                doOnTreeItemSelectedTree(event.getSelectedItem());
            }
        });

        this.scalpel = scalpel;

        homeTreeItem = new TreeItem("Home");
        homeTreeItem.setWidget(new Hyperlink("Home", "Scalpel Home"));
        tree.addItem(homeTreeItem);

        usersTreeItem = new TreeItem("Users");
        usersTreeItem.setWidget(new Hyperlink("Users", "User Management"));
        usersTreeItem.setUserObject("user_menu");
        tree.addItem(usersTreeItem);

        searchTreeItem = new TreeItem("Search");
        searchTreeItem.setWidget(new Hyperlink("Search", "Search User"));
        searchTreeItem.setUserObject("search_user");
        usersTreeItem.addItem(searchTreeItem);

        editTreeItem = new TreeItem("Edit");
        editTreeItem.setWidget(new Hyperlink("Edit", "Edit User"));
        editTreeItem.setUserObject("edit_user");
        usersTreeItem.addItem(editTreeItem);

        addTreeItem = new TreeItem("Add");
        addTreeItem.setWidget(new Hyperlink("Add", "Add User"));
        addTreeItem.setUserObject("add_user");
        usersTreeItem.addItem(addTreeItem);
    }

    protected void doOnTreeItemSelectedTree(final TreeItem item) {
        if (item == searchTreeItem) {
            SearchUsers screen = new SearchUsers();
            scalpel.addScreen(screen, "Search Users");
        } else if (item.getUserObject().equals("add_user")) {
            AddUsers screen = new AddUsers();
            scalpel.addScreen(screen, "Add User");
        } else if (item.getUserObject().equals("user_menu")) {
            usersTreeItem.setState(true);
        }
    }
}
